package action;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.servlet.http.HttpServletRequest;

import reflect.ReflectHelper;

/**
 * ActionSupport or Servlet Helper 参数处理填充
 * 
 * @author liuding,2012-3-31
 * 
 *         $LastChangedDate$ $Author$
 */
public class ActionHelper {

	/**
	 * 传入HttpServletRequest对象及完整类名(带包名)， 自动获取参数返回已填充的className实例
	 * 运用前提：className为标准的Java bean，并且传递的参数名和 类中声明的变量名拼写一致
	 * 
	 * @param req
	 * @param className
	 * @return
	 * @author liuding,2012-3-31
	 */
	public static Object getObject(HttpServletRequest req, String className) {
		Class c = null;
		try {
			c = Class.forName(className); // className
											// 为类路径，包含包名，例如java.lang.String
		} catch (ClassNotFoundException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		Object obj4 = null;
		try {
			obj4 = c.newInstance();
		} catch (InstantiationException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		} catch (IllegalAccessException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		try {
			req.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String prefix = "set";
		String methodName;
		String fieldName;
		String paramType;
		Method ms[] = null;
		Class obj = null;
		try {
			obj = Class.forName(className);
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			ms = obj.getMethods();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (ms == null) { // 获取方法失败或没有方法
			return null;
		}
		for (Method m : ms) {
			methodName = m.getName();
			if (methodName.startsWith(prefix)
					&& ReflectHelper.isDefineMethod(methodName)) {
				fieldName = methodName.substring(prefix.length(), methodName
						.length());
				fieldName = (String) (fieldName.charAt(0) + "").toLowerCase()
						+ fieldName.substring(1, fieldName.length());
				if (req.getParameter(fieldName) != null) {
					try {
						if (m.getParameterTypes().length >= 1) {
							paramType = m.getParameterTypes()[0] + "";
							if (paramType.equals("int")) {
								m.invoke(obj4, Integer.parseInt(req
										.getParameter(fieldName)));
							}else if(paramType.contains("String")){
								try {
									m.invoke(obj4, new String(req.getParameter(fieldName).getBytes("ISO-8859-1"), "UTF-8"));
								} catch (UnsupportedEncodingException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							}else if(paramType.contains("float")){
                                m.invoke(obj4, Float.parseFloat(req
                                        .getParameter(fieldName)));
                            }else if(paramType.contains("double")){
                                m.invoke(obj4, Double.parseDouble(req
                                        .getParameter(fieldName)));
                            }else if(paramType.contains("long")){
                                m.invoke(obj4, Long.parseLong((req
                                        .getParameter(fieldName))));
                            }else {
								m.invoke(obj4, req.getParameter(fieldName));
							}
                            System.out.println(m.getName()+":"+req.getParameter(fieldName)+"{paramType:" + paramType+"}");
						} else {
							// 没有参数
							m.invoke(obj4, null);
						}

					} catch (IllegalArgumentException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (InvocationTargetException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
		return obj4;
	}
}
